package com.fsnbigdata.service;

import com.fsnbigdata.domain.Enterprise;
import com.fsnbigdata.domain.EnterpriseProduct;
import com.fsnbigdata.domain.Product;
import com.fsnbigdata.domain.ProductEnterprise;
import com.fsnbigdata.domain.dto.OrgProdDTO;
import com.fsnbigdata.service.api.EnterpriseService;
import com.fsnbigdata.service.api.ProductService;
import io.springlets.data.domain.GlobalSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

@Component
public class EnterpriseProcessor implements ItemProcessor<OrgProdDTO, OrgProdDTO> {

    private static final Logger log = LoggerFactory.getLogger(EnterpriseProcessor.class);

    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    ProductService productService;

    @Override
    public OrgProdDTO process(final OrgProdDTO orgProd) throws Exception {
        log.info("{} {}", orgProd.getOrgName(), orgProd.getProductName());

        Page<Enterprise> orgCheck = enterpriseService.findAll(new GlobalSearch(orgProd.getOrgName()), new PageRequest(0, 20));
        Enterprise enterprise = null;
        if (!orgCheck.hasContent()) {
            enterprise = new Enterprise();
            enterprise.setName(orgProd.getOrgName());
            enterprise.setAddress(orgProd.getAddress());

            enterpriseService.save(enterprise);
        }else{
            enterprise = orgCheck.getContent().get(0);

            enterprise.setName(orgProd.getOrgName());
            enterprise.setAddress(orgProd.getAddress());

            enterpriseService.save(enterprise);
        }

        Page<Product> prodCheck = productService.findAll(new GlobalSearch(orgProd.getProductName()), new PageRequest(0, 20));
        Product product = null;
        if (!prodCheck.hasContent()) {
            product = new Product();
            product.setName(orgProd.getProductName());
            product.setBarcode(orgProd.getBarcode());
            product.setBrand(orgProd.getBrand());
            product.setIngredients(orgProd.getIngredients());
            product.setNutrition(orgProd.getNutrition());
            product.setTargetGroup(orgProd.getTargetGroup());
            product.setSpecification(orgProd.getSpecification());

            productService.save(product);
        }else{
            product = prodCheck.getContent().get(0);

            product.setName(orgProd.getProductName());
            product.setBarcode(orgProd.getBarcode());
            product.setBrand(orgProd.getBrand());
            product.setIngredients(orgProd.getIngredients());
            product.setNutrition(orgProd.getNutrition());
            product.setTargetGroup(orgProd.getTargetGroup());
            product.setSpecification(orgProd.getSpecification());

            productService.save(product);
        }

        EnterpriseProduct enterpriseProduct = new EnterpriseProduct();
        enterpriseProduct.setName(product.getName());
        enterpriseProduct.setBarcode(product.getBarcode());
        enterpriseProduct.setProductId(product.getId().toString());
        enterpriseProduct.setEnterprise(enterprise);
        enterprise.getProducts().add(enterpriseProduct);

        enterpriseService.save(enterprise);

        ProductEnterprise productEnterprise = new ProductEnterprise();
        productEnterprise.setEnterpriseId(enterprise.getId().toString());
        productEnterprise.setName(enterprise.getName());
        productEnterprise.setProduct(product);
        product.getEnterprises().add(productEnterprise);

        productService.save(product);

        return orgProd;
    }

}
